package com.lagou.webwork.servlet.classes; /**
 * @author sjp
 * @date 2021/6/4 17:04
 * @description
 */

import com.lagou.webwork.model.Class;
import com.lagou.webwork.model.ListResult;
import com.lagou.webwork.model.Result;
import com.lagou.webwork.model.Student;
import com.lagou.webwork.service.ClassService;
import com.lagou.webwork.service.StudentService;
import com.lagou.webwork.utils.JSONUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet(name = "GetClassListServlet", value = "/getClassList")
public class GetClassListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService service = new ClassService();
        request.setCharacterEncoding("utf-8");
        String paramsList = request.getParameter("paramsList");
        int page = Integer.valueOf(request.getParameter("page"));
        int number = Integer.valueOf(request.getParameter("number"));

        List<Map<String, Object>> maps = JSONUtils.toList(paramsList);
        ListResult<Class> classList = service.getClassList(maps,page,number);


        Result<Class> result = new Result<>("200", new ArrayList<>(), 0, "");
        if (classList.getTotalCount()>0){
            result.setDataList(classList.getDataList());
            result.setTotalCount(classList.getTotalCount());
            result.setMsg("获取列表信息成功!");
        }else{
            result.setMsg("获取列表失败，请刷新后再试。");
        }

        //设置编码格式
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        //输出给前端
        PrintWriter writer = response.getWriter();
        writer.print(result.toString());
        writer.flush();
    }
}
